package com.test.ml.yqs.utills;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.github.pagehelper.PageInfo;
import com.test.ml.yqs.enums.RequestStatusEnum;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * ResultJson
 * </p>
 * <p>
 * 统一restful接口返回
 * </p>
 * 
 * @date Jun 9, 2017
 */
public class ResultJson implements Serializable {

    private static final long serialVersionUID = -5425361471916352020L;

    /** 状态 */
    private Integer result;
    /** 消息 */
    private String msg;

    private Integer errorCode;
    /** 数据 */
    @JsonInclude(Include.NON_NULL)
    private Object data;
    /** 总条数 */
    @JsonInclude(Include.NON_NULL)
    private Long total;
    /** 当前页 */
    @JsonInclude(Include.NON_NULL)
    private Integer pageIndex;
    /** 页容量 */
    @JsonInclude(Include.NON_NULL)
    private Integer pageSize;

   

    public Integer getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }

    public ResultJson() {
    }

    public ResultJson(Integer result, String msg) {
        super();
        this.result = result;
        this.msg = msg;
    }

    public ResultJson(Integer result, String msg, Object data, Long total,
                      Integer pageIndex, Integer pageSize) {
        this.result = result;
        this.msg = msg;
        this.data = data;
        this.total = total;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }

    public ResultJson(Integer result, Integer errorCode, String msg,
                      Object data, Long total, Integer pageIndex, Integer pageSize) {
        this.result = result;
        this.errorCode = errorCode;
        this.msg = msg;
        this.data = data;
        this.total = total;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }
    
    /**
     * <p>只返回错误信息与错误码</p>
     * @param msg 待返回消息
     * @param errorCode 错误码
     * @return ResultJson
     */
    public static  ResultJson buildErrorWithErrorCode(String msg, Integer errorCode){
        return new ResultJson(RequestStatusEnum.FAIL_FIELD.getStatus(), errorCode ,msg, null, null, null, null);
    }


    /**
     * <p>
     * 成功返回信息
     * </p>
     * 
     * @param msg
     *            待返回消息
     * @return ResultJson
     */
    public static ResultJson buildSuccess(String msg) {
        return new ResultJson(RequestStatusEnum.SUCCESS.getStatus(), msg, null,
                null, null, null);
    }
    public static ResultJson buildSuccess() {
        return new ResultJson(RequestStatusEnum.SUCCESS.getStatus(), "操作成功", null,
                null, null, null);
    }

    public static ResultJson buildSuccess(String msg,Object data) {
        return new ResultJson(RequestStatusEnum.SUCCESS.getStatus(), "操作成功", data,
                null, null, null);
    }

    /**
     * <p>
     * 成功返回数据和条数
     * </p>
     * 
     * @param msg
     *            待返回消息
     * @param data
     *            待返回数据
     * @param total
     *            总条数
     * @return ResultJson
     */
    public static ResultJson buildSuccess(String msg, Object data, Long total) {
        return new ResultJson(RequestStatusEnum.SUCCESS.getStatus(), msg, data,
                total, null, null);
    }

    /**
     * <p>
     * 成功返回分页相关
     * </p>
     * 
     * @param msg
     *            待返回消息
     * @param pageinfo
     *            待返回PageInfo
     * @return ResultJson
     */
    public static ResultJson buildSuccess(String msg, PageInfo<?> pageinfo) {
        Object data = pageinfo.getList();
        Long total = pageinfo.getTotal();
        Integer pageIndex = pageinfo.getPageNum();
        Integer pageSize = pageinfo.getPageSize();
        return new ResultJson(RequestStatusEnum.SUCCESS.getStatus(), msg, data,
                total, pageIndex, pageSize);
    }

    /**
     * <p>
     * 只返回错误信息
     * </p>
     * 
     * @param msg
     *            待返回消息
     * @return ResultJson
     */
    public static ResultJson buildError(String msg) {
        return new ResultJson(RequestStatusEnum.FAIL_FIELD.getStatus(), msg,
                null, null, null, null);
    }
    public static ResultJson buildError() {
    	return new ResultJson(RequestStatusEnum.FAIL_FIELD.getStatus(), "操作失败",
    			null, null, null, null);
    }
    
    /**
     * 返回错误信息，包括错误消息，错误数据
     * @param msg
     * @param data
     * @return
     */
    public static ResultJson buildError(String msg, Object data) {
        return new ResultJson(RequestStatusEnum.FAIL_FIELD.getStatus(), msg, data,
                null, null, null);
    }

    /**
     * <p>
     * 参数失败
     * </p>
     * 
     * @param msg
     *            待返回消息
     * @return ResultJson
     */
    public static ResultJson buildParamError(String msg) {
        return new ResultJson(RequestStatusEnum.FAIL_PARAM.getStatus(), msg,
                null, null, null, null);
    }

    /**
     * <p>
     * 自定义状态返回数据
     * </p>
     * 
     * @param result
     *            状态
     * @param msg
     *            消息
     * @param data
     *            数据
     * @return ResultJson
     */
    public static ResultJson buildResult(RequestStatusEnum result, String msg,
            Object data) {
        return new ResultJson(result.getStatus(), msg, data, null, null, null);
    }

    /**
     * <p>
     * 自定义状态返回分页
     * </p>
     * 
     * @param result
     *            状态
     * @param msg
     *            消息
     * @param data
     *            数据
     * @param total
     *            总条数
     * @param pageIndex
     *            当前页
     * @param pageSize
     *            页容量
     * @return ResultJson
     */
    public static ResultJson buildResult(RequestStatusEnum result, String msg,
            Object data, Long total, Integer pageIndex, Integer pageSize) {
        return new ResultJson(result.getStatus(), msg, data, total, pageIndex,
                pageSize);
    }

    public static boolean isSucess(ResultJson resultJson){
        return resultJson.result.equals(1);
    }

    /**
     * <p>
     * 绑定表单日期格式
     * </p>
     * 
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                dateFormat, true));
    }

    /* getters & setters */
    public Integer getResult() {
        return result;
    }

    public void setResult(Integer result) {
        this.result = result;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString() {
        return "ResultJson [result=" + result + ", msg=" + msg + ", data="
                + data + ", total=" + total + ", pageIndex=" + pageIndex
                + ", pageSize=" + pageSize + "]";
    }

}
